<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
      <div class="head-container">
        <el-input
          v-model="locationName"
          placeholder="请输入位置信息"
          clearable
          size="small"
          prefix-icon="el-icon-search"
          style="margin-bottom: 20px"
        />
      </div>
      <div class="head-container">
        <el-tree
          :data="deptOptions"
          :props="defaultProps"
          :expand-on-click-node="false"
          :filter-node-method="filterNode"
          ref="tree"
          node-key="id"
          default-expand-all
          highlight-current
          @node-click="handleNodeClick"
        />
      </div>
      </el-col>
      <el-col :span="20" :xs="24">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="deviceCode">
        <el-inpu
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['management:device:add']"
        >新增设备</el-button>
      </el-col>
    </el-row>


          <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
              <el-table-column label="序号" align="center" prop="id"/>
              <el-table-column label="设备名称" align="center" prop="deviceName" width="150" />
              <el-table-column label="设备状态" align="center">
                <template slot-scope="scope">
                  <el-tag type="success"><span>{{selectDictLabel(dict.type.device_status,scope.row.deviceStatus)}}</span></el-tag>
                </template>
              </el-table-column>
              <el-table-column label="设备位置" align="center" prop="location" width="180"/>
              <el-table-column label="设备编号" align="center" prop="deviceCode"  width="150"/>
            <el-table-column label="设备类型" align="center">
              <template slot-scope="scope">
                <span>{{selectDictLabel(dict.type.device_type,scope.row.deviceType)}}</span>
              </template>
            </el-table-column>
            <el-table-column label="设备剩余金额" align="center" prop="deviceBalance" width="120"/>
            <el-table-column label="设备剩余量" align="center" prop="deviceSurplus" width="120"/>
            <el-table-column label="水表阀门状态" align="center"width="120">
              <template slot-scope="scope">
                <el-tag :type="(scope.row.sluiceStatus=='0'?'success':(scope.row.sluiceStatus=='1'?'info':'info'))"><span>{{selectDictLabel(dict.type.sluice_status,scope.row.sluiceStatus)}}</span></el-tag>
              </template>
            </el-table-column>
            <el-table-column label="电表总状态" align="center" width="120">
              <template slot-scope="scope">
                <el-tag :type="scope.row.electricityStatus=='1'?'success':'info'"><span>{{selectDictLabel(dict.type.electricity_status,scope.row.electricityStatus)}}</span></el-tag>
              </template>
            </el-table-column>
            <el-table-column label="电表A路状态" align="center" width="120">
              <template slot-scope="scope">
                <el-tag :type="scope.row.electricityStatus=='1'?'success':'info'"><span>{{selectDictLabel(dict.type.electricity_status,scope.row.electricityAStatus)}}</span></el-tag>
              </template>
            </el-table-column>
            <el-table-column label="电表B路状态" align="center" width="120">
              <template slot-scope="scope">
                <el-tag :type="scope.row.electricityStatus=='1'?'success':'info'"><span>{{selectDictLabel(dict.type.electricity_status,scope.row.electricityBStatus)}}</span></el-tag>
              </template>
            </el-table-column>
            <el-table-column label="入网时间" align="center" prop="modifyTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.modifyTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column
              fixed="right"
              label="操作"
              width="220">
              <template slot-scope="scope">
                <el-tooltip content="修改" align="center" class-name="small-padding fixed-width">
                <el-button type="primary" icon="el-icon-edit" circle @click="handleUpdate(scope.row)"></el-button>
                </el-tooltip>
                <el-button type="danger" icon="el-icon-delete" circle @click="handleDelete(scope.row)"></el-button>
                <el-button type="warning" icon="el-icon-refresh" circle @click="getList()"></el-button>
                <el-button type="info" icon="el-icon-setting" circle>
                </el-button>
              </template>
            </el-table-column>
          </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
      </el-col>
    </el-row>

    <!-- 添加或修改设备网关配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="网关ID" prop="gatewayId">
          <el-input v-model="form.gatewayId" placeholder="请输入网关ID" />
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备位置" prop="deviceLocation">
          <el-input v-model="form.deviceLocation" placeholder="请输入设备位置" />
        </el-form-item>
        <el-form-item label="充值次数" prop="rechargeCount">
          <el-input v-model="form.rechargeCount" placeholder="请输入充值次数" />
        </el-form-item>
        <el-form-item label="设备剩余金额" prop="deviceBalance">
          <el-input v-model="form.deviceBalance" placeholder="请输入设备剩余金额" />
        </el-form-item>
        <el-form-item label="设备剩余量" prop="deviceSurplus">
          <el-input v-model="form.deviceSurplus" placeholder="请输入设备剩余量" />
        </el-form-item>
        <el-form-item label="总量" prop="totle">
          <el-input v-model="form.totle" placeholder="请输入总量" />
        </el-form-item>
        <el-form-item label="设备ip" prop="deviIp">
          <el-input v-model="form.deviIp" placeholder="请输入设备ip" />
        </el-form-item>
        <el-form-item label="设备用户名" prop="deviUsername">
          <el-input v-model="form.deviUsername" placeholder="请输入设备用户名" />
        </el-form-item>
        <el-form-item label="设备密码" prop="deviPassword">
          <el-input v-model="form.deviPassword" placeholder="请输入设备密码" />
        </el-form-item>
        <el-form-item label="设备sdk端口号" prop="deviSdkPort">
          <el-input v-model="form.deviSdkPort" placeholder="请输入设备sdk端口号" />
        </el-form-item>
        <el-form-item label="常开时段" prop="deviOpenAlwaysTimeIndex">
          <el-input v-model="form.deviOpenAlwaysTimeIndex" placeholder="请输入常开时段" />
        </el-form-item>
        <el-form-item label="常闭时段" prop="deviCloseAlwaysTimeIndex">
          <el-input v-model="form.deviCloseAlwaysTimeIndex" placeholder="请输入常闭时段" />
        </el-form-item>
        <el-form-item label="空间位置" prop="bsloId">
          <el-input v-model="form.bsloId" placeholder="请输入空间位置" />
        </el-form-item>
        <el-form-item label="设备其他信息" prop="deviOtherInfo">
          <el-input v-model="form.deviOtherInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="剩余量单位" prop="surplusUnit">
          <el-input v-model="form.surplusUnit" placeholder="请输入剩余量单位" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifyTime">
          <el-date-picker clearable
            v-model="form.modifyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改人" prop="modifyBy">
          <el-input v-model="form.modifyBy" placeholder="请输入修改人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/management/device";

export default {
  name: "Device",
  dicts:['device_status','device_type','sluice_status','electricity_status'],
  data() {
    return {
      deptOptions:undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      locationName:undefined,
      // 总条数
      total: 0,
      // 设备网关配置表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gatewayId: null,
        location:null,
        deviceCode: null,
        deviceType: null,
        deviceName: null,
        deviceStatus: null,
        deviceLocation: null,
        rechargeCount: null,
        deviceBalance: null,
        deviceSurplus: null,
        totle: null,
        deviIp: null,
        deviUsername: null,
        deviPassword: null,
        deviSdkPort: null,
        deviAccessType: null,
        deviOpenAlwaysTimeIndex: null,
        deviCloseAlwaysTimeIndex: null,
        bsloId: null,
        deviOtherInfo: null,
        surplusUnit: null,
        sluiceStatus: null,
        electricityStatus: null,
        electricityAStatus: null,
        electricityBStatus: null,
        modifyTime: null,
        modifyBy: null
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备网关配置列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gatewayId: null,
        deviceCode: null,
        deviceType: null,
        deviceName: null,
        deviceStatus: null,
        deviceLocation: null,
        rechargeCount: null,
        deviceBalance: null,
        deviceSurplus: null,
        totle: null,
        deviIp: null,
        deviUsername: null,
        deviPassword: null,
        deviSdkPort: null,
        deviAccessType: null,
        deviOpenAlwaysTimeIndex: null,
        deviCloseAlwaysTimeIndex: null,
        bsloId: null,
        deviOtherInfo: null,
        surplusUnit: null,
        sluiceStatus: null,
        electricityStatus: null,
        electricityAStatus: null,
        electricityBStatus: null,
        createTime: null,
        createBy: null,
        modifyTime: null,
        modifyBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备网关配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备网关配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备网关配置编号为"' + ids + '"的数据项？').then(function() {
        return delDevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('management/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleNodeClick(data) {
      this.queryParams.id = data.id;
      this.handleQuery();
    },
  }
};
</script>
